package com.example.callcenter.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.callcenter.entity.CallRecord;
import com.example.callcenter.entity.SysUser;
import com.example.callcenter.dto.CallRecordDTO;
import com.example.callcenter.mapper.CallRecordMapper;
import com.example.callcenter.service.ICallRecordService;
import com.example.callcenter.util.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class CallRecordServiceImpl extends ServiceImpl<CallRecordMapper, CallRecord> implements ICallRecordService {

    @Override
    public IPage<CallRecord> getByTaskId(Long taskId, int pageNum, int pageSize) {
        IPage<CallRecord> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectByTaskId(page, taskId);
    }

    @Override
    public IPage<CallRecord> getBySalesId(Long salesId, int pageNum, int pageSize) {
        IPage<CallRecord> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectBySalesId(page, salesId);
    }

    @Override
    @Transactional
    public void saveRecord(CallRecordDTO recordDTO) {
        CallRecord record = new CallRecord();
        record.setTaskId(recordDTO.getTaskId());
        record.setCustomerId(recordDTO.getCustomerId());
        record.setSalesId(UserContext.getUserId()); // 当前登录销售ID
        record.setDuration(recordDTO.getDuration());
        record.setResult(recordDTO.getResult());
        record.setRemark(recordDTO.getRemark());
        record.setCallTime(LocalDateTime.now());
        record.setCreateTime(LocalDateTime.now());
        baseMapper.insert(record);
    }
}